const {get_blood_war_info,get_blood_war_info_merge,get_robot_flag,get_once_active } = require('../dao/redis_dao');
const {getRobotInfo,getUserToBackList,deleteUserIdToBackList,getUserIdToIdCard} = require('../dao/dao');
const {getRandomItemsShuffle} = require('../../utils/sort');
var task_api = {};

//获取铁血战争统计信息
task_api.get_blood_war_info_one = function (key, callback) {
    get_blood_war_info(key).then(function (result) {
        callback(result);
    }).catch(error=>{
        callback({});
    })
}

task_api.get_robot_info = function ( callback) {
    getRobotInfo(function (result) {
        if(result !==0 && result !==1){
            const robotArray = [];
            result = getRandomItemsShuffle(result,20)
            for (let i = 0; i < result.length; i++) {
                const userInfo = {
                    userId: result[i].userId,
                    nickname: result[i].nickname,
                    rewardCoin: 0,
                    headimgurl: result[i].headimgurl,
                }
                robotArray.push(userInfo);
            }
            callback(robotArray);
        }
    })
}

//获取铁血战争统计信息
task_api.get_blood_war_info = function (req, callback) {
    var key = 'iron_blood_war_' + req.query.type;
    if (Number(req.query.flag) === -1) {
        let list = [];
        key = 'last_iron_blood_war_' + req.query.type;
        let hisKey = 'history_iron_blood_war_' + req.query.type;
        get_blood_war_info_merge(key, hisKey).then(function (result) {
            callback(result);
        }).catch(error=>{
            console.error(error);
        });

    } else {
        //已经发放的
        get_blood_war_info(key).then(function (result) {
            console.log("get_blood_war_info返回数据",result);
            if(result == 0 || result == 1){
                callback(result);
            } else {
                let robotArray = [];
                get_robot_flag().then(cb=>{
                    console.log("机器人占位开关",cb);
                    if (Number(cb) === 1 ) { //这里如果用===需要将cb指定类型，如果用==就不需要
                        console.log("进入到这个判断里面.......");
                        getRobotInfo(function (dbRes) {
                            console.log("getRobotInfo 回调被调用，dbRes的数量为:", dbRes.length);
                            if (dbRes) {
                                console.log(dbRes);
                                dbRes = getRandomItemsShuffle(dbRes,20)
                                console.log("getRandomItemsShuffle 调用，dbRes的数量为:", dbRes.length);
                                for (let i = 0; i < dbRes.length; i++) {
                                    const userInfo = {
                                        userId: dbRes[i].id,
                                        nickname: dbRes[i].nickname,
                                        rewardCoin: 0,
                                        headimgurl: dbRes[i].headimgurl,
                                    }
                                    robotArray.push(userInfo);
                                }
                                robotArray.push(...result);
                                callback(robotArray);
                            } else {
                                console.log("没有设置机器人");
                                callback(result);
                            }
                        });
                    } else {
                        callback(result);
                    }
                });
            }
        }).catch(error=>{
            console.error(error);
        });
    }
}

task_api.get_user_black_list = function (userId, callback) {
    getUserToBackList(userId,function (result) {
        callback(result);
    })
}

task_api.get_robot_flag = function (callback) {
    get_robot_flag().then(function (result) {
        callback(result);
    })
}

task_api.get_user_once = function(propId, callback) {
    const onceActiveMap = new Map();
    onceActiveMap.set(1,'first_recharge_1');
    onceActiveMap.set(2,'new_gift_1');
    onceActiveMap.set(3,'new_gift_2');
    onceActiveMap.set(4,'new_gift_3');
    onceActiveMap.set(23,'pri_card');
    onceActiveMap.set(24,'pri_card');
    onceActiveMap.set(28,'pri_card');
    onceActiveMap.set(29,'vip1');
    onceActiveMap.set(30,'vip2');
    onceActiveMap.set(31,'vip3');
    onceActiveMap.set(32,'vip4');
    onceActiveMap.set(33,'vip5');
    onceActiveMap.set(34,'vip6');
    onceActiveMap.set(35,'vip7');
    onceActiveMap.set(36,'vip8');
    onceActiveMap.set(37,'vip9');
    onceActiveMap.set(38,'vip10');

    if(onceActiveMap.has(Number(propId))){
        get_once_active().then(function (result) {
            const map = new Map();
            console.log("get_once_active返回数据",result);
            result.forEach(item => {
                Object.entries(item).forEach(([key, value]) => {
                    map.set(key, value);
                });
            });
            console.log("转换之后的数据",map);
            const  value = map.get(onceActiveMap.get(Number(propId)));
            console.log("值为",value);
            if(value == 0){
                console.log("可以在继续充值",value);
                callback(0);
            } else {
                console.log("已经不能再充值了",value);
                callback(1);
            }
        }).catch(error=>{
            console.error(error);
        });
    }
}

task_api.get_user_test = function (req, callback) {
    //判断充值用户是否在黑名单中，如果存在则剔除出黑名单
    getUserIdToIdCard(req.query.userId, function (result) {
        console.log("getUserIdToIdCard返回数据",result);
        if(result == -1){
            callback(-1);
            console.error('充值回调时，查询用户身份证信息失败，userId:', result.userId)
        }
        if(result){
            console.log('进入到这里');
            deleteUserIdToBackList(result[0].id_card,function (res){
                if(res == 0){
                    console.log('剔除黑名单成功');
                    callback(1);
                } else
                    console.log('剔除黑名单失败');
            });
        }
    });
}

module.exports = task_api;